package com.budwk.app.sqfw.services.impl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.budwk.app.base.gmpass.SM3Util;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.sqfw.dto.*;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.enums.SqfwRallyEnum;
import com.budwk.app.sqfw.models.*;
import com.budwk.app.sqfw.services.*;
import com.budwk.app.sqfwV2.dto.RallyCustomDTO;
import com.budwk.app.sqfwV2.dto.TemplateFileDTO;
import com.budwk.app.sqfwV2.dto.RallyPdfSaveDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.dto.vo.SummaryQueryReqVo;
import com.budwk.app.sqfwV2.enums.SqfwRallyEnums;
import com.budwk.app.sys.models.Sys_dict;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.DynamicExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.common.exception.BaseException;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.cri.SqlExpression;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import java.util.*;


/**
 * @title: HuaweiPushServiceImpl
 * @Author Tan
 * @Date: 2023/7/25 17:10
 * @Version 1.0
 */

@Slf4j
@IocBean(args = {"refer:dao"})
public class SqfwRallyServiceImpl extends BaseServiceImpl<Sqfw_rally> implements SqfwRallyService {

    public SqfwRallyServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private SqfwServicerecordService sqfwServicerecordService;

    @Inject
    private SysDictService sysDictService;

    @Inject
    private SqfwRallySignService sqfwRallySignService;
    @Inject
    private SqfwRallySignForAdminService sqfwRallySignForAdminService;


    @Inject
    private SysUserService sysUserService;

    @Inject
    private SqfwReportFileService sqfwReportFileService;

    @Inject
    private PropertiesProxy properties;

    @Override
    @Aop(TransAop.READ_COMMITTED)
    @SaCheckLogin
    public Sqfw_rally createOne(Sqfw_rally sqfwRally) {
        if (ObjectUtil.isEmpty(sqfwRally)) {
            throw new ServiceException("对象参数为空");
        }
        Sys_user user = sysUserService.getUserById(SecurityUtil.getUserId());
        if (StrUtil.isEmpty(user.getProvince()) ||
                StrUtil.isEmpty(user.getCity()) ||
                StrUtil.isEmpty(user.getDistrict()) ||
                StrUtil.isEmpty(user.getCounty()) ||
                StrUtil.isEmpty(user.getCounty())) {
            throw new ServiceException("当前登录用户所属区域为空");
        }
        if (StrUtil.isEmpty(user.getUnitName())) {
            throw new ServiceException("当前登录用户所属单位为空");
        }

        String rallyIncludeArea = getArea(user);


        if (ObjectUtil.isEmpty(sqfwRally.getRallyType())) {
            throw new ServiceException("活动类型为空");
        }


        if (sqfwRally.getRallyType() == SqfwRallyEnum.TONGZHI.getCode()) {
            sqfwRally.setRallyIncludeArea(null);
            sqfwRally.setRallyIncludeSex(null);
            sqfwRally.setRallyIncludeRange(null);
            sqfwRally.setRallyIncludeAge(null);
        } else {
            List<String> ageList = JSONUtil.parseArray(sqfwRally.getRallyIncludeAge()).toList(String.class);
            if (CollectionUtil.isEmpty(ageList)) {
                sqfwRally.setRallyIncludeAge(null);
            }
            List<String> sexList = JSONUtil.parseArray(sqfwRally.getRallyIncludeSex()).toList(String.class);
            if (CollectionUtil.isEmpty(sexList)) {
                sqfwRally.setRallyIncludeSex(null);
            }
            if (StrUtil.isBlank(sqfwRally.getPeoLimit())) {
                sqfwRally.setPeoLimit(null);
            }
        }


        sqfwRally.setProvince(user.getProvince());
        sqfwRally.setCity(user.getCity());
        sqfwRally.setCommunity(user.getCommunity());
        sqfwRally.setDistrict(user.getDistrict());
        sqfwRally.setCounty(user.getCounty());
        sqfwRally.setRallyReleaseDept(user.getUnitName());
        sqfwRally.setRallyIncludeArea(rallyIncludeArea);
        sqfwRally.setRallyReleaseUserName(user.getUsername());
        sqfwRally.setRallyReleaseUserId(user.getId());
        sqfwRally.setCreateTime(new Date());
        sqfwRally.setFinishStatus("new");
        this.insert(sqfwRally);

        Sqfw_servicerecord sqfwServicerecord = new Sqfw_servicerecord();
        sqfwServicerecord.setType("发布活动");
        sqfwServicerecord.setCustomer_name(user.getUsername());
        sqfwServicerecord.setCustomer_phone(user.getLoginname());
        sqfwServicerecord.setCustomer_need(sqfwRally.getRallyName());
        sqfwServicerecord.setFinishStatus("new");
        sqfwServicerecord.setService_phone(user.getLoginname());
        sqfwServicerecord.setCopyPhone(user.getLoginname());
        sqfwServicerecord.setProvince(user.getProvince());
        sqfwServicerecord.setService_phone(user.getLoginname());
        sqfwServicerecord.setCity(user.getCity());
        sqfwServicerecord.setDistrict(user.getDistrict());
        sqfwServicerecord.setCounty(user.getCounty());
        sqfwServicerecord.setCommunity(user.getCommunity());
        sqfwServicerecord.setCreatedBy(user.getId());
        sqfwServicerecord.setRallyId(sqfwRally.getId());
        sqfwServicerecordService.insert(sqfwServicerecord);

        return sqfwRally;
    }

    @Override
    @SaCheckLogin
    public Sqfw_rally createOneJson(SqfwRallyUpdateDTO sqfwRallyUpdateDTO) {
        if(ObjectUtil.isEmpty(sqfwRallyUpdateDTO.getRallyPdfSaveDTO())){
            throw new BaseException("rallyPdfSaveDTO不能为空");
        }
        Sys_user user = sysUserService.getUserById(SecurityUtil.getUserId());
        if (StrUtil.isEmpty(user.getProvince()) ||
                StrUtil.isEmpty(user.getCity()) ||
                StrUtil.isEmpty(user.getDistrict()) ||
                StrUtil.isEmpty(user.getCounty()) ||
                StrUtil.isEmpty(user.getCounty())) {
            throw new ServiceException("当前登录用户所属区域为空");
        }
        if (StrUtil.isEmpty(user.getUnitName())) {
            throw new ServiceException("当前登录用户所属单位为空");
        }
        sqfwRallyUpdateDTO.getRallyPdfSaveDTO().setRallyReleaseUserName(user.getUsername());
        sqfwRallyUpdateDTO.getRallyPdfSaveDTO().setPhone(user.getLoginname());
        sqfwRallyUpdateDTO.setRallyContentJson(JSONUtil.toJsonStr(sqfwRallyUpdateDTO.getRallyPdfSaveDTO()));
        Sqfw_rally sqfwRally=new Sqfw_rally();
        BeanUtil.copyProperties(sqfwRallyUpdateDTO,sqfwRally);
        if (sqfwRally.getRallyType() == SqfwRallyEnum.JSONBN.getCode()) {
            sqfwRally.setRallyIncludeArea(null);
            sqfwRally.setRallyIncludeSex(null);
            sqfwRally.setRallyIncludeRange(null);
            sqfwRally.setRallyIncludeAge(null);
        } else {
            List<String> ageList = JSONUtil.parseArray(sqfwRally.getRallyIncludeAge()).toList(String.class);
            if (CollectionUtil.isEmpty(ageList)) {
                sqfwRally.setRallyIncludeAge(null);
            }
            List<String> sexList = JSONUtil.parseArray(sqfwRally.getRallyIncludeSex()).toList(String.class);
            if (CollectionUtil.isEmpty(sexList)) {
                sqfwRally.setRallyIncludeSex(null);
            }
            if (StrUtil.isBlank(sqfwRally.getPeoLimit())) {
                sqfwRally.setPeoLimit(null);
            }
        }
        String rallyIncludeArea = getArea(user);
        sqfwRally.setProvince(user.getProvince());
        sqfwRally.setCity(user.getCity());
        sqfwRally.setCommunity(user.getCommunity());
        sqfwRally.setDistrict(user.getDistrict());
        sqfwRally.setCounty(user.getCounty());
        sqfwRally.setRallyReleaseDept(user.getUnitName());
        sqfwRally.setRallyIncludeArea(rallyIncludeArea);
        sqfwRally.setRallyReleaseUserName(user.getUsername());
        sqfwRally.setRallyReleaseUserId(user.getId());
        sqfwRally.setCreateTime(new Date());
        sqfwRally.setFinishStatus("new");
        this.insert(sqfwRally);
        pushRally(sqfwRally);
        return sqfwRally;
    }

    @Override
    public Sqfw_rally getOne(SqfwRallyDTO sqfwRally) {
        Sqfw_rally rally = this.fetch(sqfwRally.getId());
        if (ObjectUtil.isEmpty(sqfwRally)) {
            throw new ServiceException("无法查找到对象");
        }
        return rally;
    }

    @Override
    public SqfwRallyUpdateDTO getOneJson(SqfwRallyDTO sqfwRally) {
        Sqfw_rally rally = this.fetch(sqfwRally.getId());
        if (ObjectUtil.isEmpty(sqfwRally)) {
            throw new ServiceException("无法查找到对象");
        }
        SqfwRallyUpdateDTO  sqfwRallyUpdateDTO = new SqfwRallyUpdateDTO();
        BeanUtil.copyProperties(rally,sqfwRallyUpdateDTO);
        RallyPdfSaveDTO rallyPdfSaveDTO=JSONUtil.toBean(rally.getRallyContentJson(),RallyPdfSaveDTO.class);
        sqfwRallyUpdateDTO.setRallyPdfSaveDTO(rallyPdfSaveDTO);
        return sqfwRallyUpdateDTO;
    }

    @Override
    @Aop(TransAop.READ_COMMITTED)
    public boolean delRally(String id) {
        Sqfw_servicerecord sqfwServicerecord = sqfwServicerecordService.fetch(id);
        if (ObjectUtil.isNotEmpty(sqfwServicerecord)) {
            if (this.delete(sqfwServicerecord.getRallyId()) > 0) {
                sqfwServicerecordService.delete(id);
                sqfwRallySignService.clear(Cnd.where(SqfwRallySign::getRallyId, "=", sqfwServicerecord.getRallyId()));
                return true;
            }
        }
        throw new BaseException("id无法关联对象");
    }

    @Override
    public boolean delRallyJson(String id) {
        this.clear(Cnd.where(Sqfw_rally::getId, "=", id));
        sqfwRallySignService.clear(Cnd.where(SqfwRallySign::getRallyId, "=", id));
        sqfwReportFileService.clear(Cnd.where(Sqfw_reportFile::getSqfwId, "=", id));
        return true;
    }

    @Override
    public boolean delRallyJson(List<String> idS) {
        this.clear(Cnd.where(Sqfw_rally::getId, "in", idS));
        sqfwRallySignService.clear(Cnd.where(SqfwRallySign::getRallyId, "in", idS));
        sqfwReportFileService.clear(Cnd.where(Sqfw_reportFile::getSqfwId, "in", idS));
        return true;
    }


    @Override
    public SqfwRallyUpdateDTO updateOne(SqfwRallyUpdateDTO sqfwRally) {
        if (sqfwRally.getRallyType() == SqfwRallyEnum.TONGZHI.getCode()) {
            sqfwRally.setRallyIncludeArea(null);
            sqfwRally.setRallyIncludeSex(null);
            sqfwRally.setRallyIncludeRange(null);
            sqfwRally.setRallyIncludeAge(null);
        } else {
            List<String> ageList = JSONUtil.parseArray(sqfwRally.getRallyIncludeAge()).toList(String.class);
            if (CollectionUtil.isEmpty(ageList)) {
                sqfwRally.setRallyIncludeAge(null);
            }
            List<String> sexList = JSONUtil.parseArray(sqfwRally.getRallyIncludeSex()).toList(String.class);
            if (CollectionUtil.isEmpty(sexList)) {
                sqfwRally.setRallyIncludeSex(null);
            }
            if (StrUtil.isBlank(sqfwRally.getPeoLimit())) {
                sqfwRally.setPeoLimit(null);
            }
            if(ObjectUtil.isNotEmpty(sqfwRally.getRallyPdfSaveDTO())){
                sqfwRally.setRallyContentJson(JSONUtil.toJsonStr(sqfwRally.getRallyPdfSaveDTO()));
            }
        }
        this.update(Chain.make("rally_include_sex", sqfwRally.getRallyIncludeSex())
                        .add("rally_include_age", sqfwRally.getRallyIncludeAge())
                        .add("rally_name", sqfwRally.getRallyName())
                        .add("rally_content", sqfwRally.getRallyContent())
                        .add("rally_start_time", sqfwRally.getRallyStartTime())
                        .add("rally_end_time", sqfwRally.getRallyEndTime())
                        .add("rally_type", sqfwRally.getRallyType())
                        .add("rally_include_range", sqfwRally.getRallyIncludeRange())
                        .add("conditions", sqfwRally.getConditions())
                        .add("full_range", sqfwRally.getFullRange())
                        .add("peo_limit", sqfwRally.getPeoLimit())
                        .add("draft", sqfwRally.getDraft())
                        .add("rally_content_json", sqfwRally.getRallyContentJson())
                        .add("eventregistrationtime", sqfwRally.getEventRegistrationTime()),
                Cnd.where("id", "=", sqfwRally.getId()));
        return sqfwRally;
    }


    @Override
    @Aop(TransAop.READ_COMMITTED)
    public Sqfw_servicerecord finishService(Sqfw_rally sqfwRally) {
        Sqfw_servicerecord sqfwServicerecord = sqfwServicerecordService.fetch(sqfwRally.getServiceId());
        if (ObjectUtil.isNotEmpty(sqfwServicerecord)) {
            sqfwServicerecord.setFinishStatus("complete");
            sqfwServicerecord.setRemarks(sqfwRally.getRemarks());
            Sqfw_rally rally = this.fetch(sqfwServicerecord.getRallyId());
            rally.setFinishStatus("complete");
            this.updateIgnoreNull(rally);
            sqfwServicerecordService.updateIgnoreNull(sqfwServicerecord);
            return sqfwServicerecord;
        }
        throw new BaseException("id无法关联对象");
    }

    @Override
    public RallyExcelDTO generateRallyExcel(String rallyId) {
        RallyExcelDTO rallyExcelDTO=new RallyExcelDTO();
        rallyExcelDTO.setExcelNname("活动人员报名信息表");
        try {
            Sqfw_rally rally = this.fetch(rallyId);
            if (ObjectUtil.isEmpty(rally)) {
                throw new ServiceException("无法找到对象");
            }
            List<Map<String, Object>> dataDynamic=new ArrayList<>();
            DynamicExcelFileCreateParamBO excelFileCreateParam = new DynamicExcelFileCreateParamBO();
            RallyPdfSaveDTO rallyPdfSaveDTO=null;
            if(StrUtil.isNotBlank(rally.getRallyContentJson())){
                rallyPdfSaveDTO=JSONUtil.toBean(rally.getRallyContentJson(),RallyPdfSaveDTO.class);
                if(CollectionUtil.isNotEmpty(rallyPdfSaveDTO.getCustom())){
                    List<RallyCustomDTO> custom = rallyPdfSaveDTO.getCustom();
                    List<Map<String, Object>> headDynamic=new ArrayList<>();
                    custom.forEach(s->{
                        Map<String, Object> head=new HashMap<>();
                        head.put("head",s.getName());
                        headDynamic.add(head);
                    });
                    excelFileCreateParam.setHeadDynamic(headDynamic);

                }

            }
            List<SqfwRallySign> sqfwRallySigns = sqfwRallySignService.query(Cnd.where(SqfwRallySign::getRallyId, "=", rallyId));
            if (CollUtil.isNotEmpty(sqfwRallySigns)) {
                // 统计未签到情况 add by LJT 2024-01-25
                List<Sqfw_rally_sign> sqfwRallySignsList = new ArrayList<Sqfw_rally_sign>();
                sqfwRallySigns.forEach(source -> {
                    Sqfw_rally_sign target = new Sqfw_rally_sign();
                    BeanUtil.copyProperties(source, target);
                    sqfwRallySignsList.add(target);
                });
                Map<String, String> timeOutPhoneMap = sqfwRallySignForAdminService.checkSignTimeout(sqfwRallySignsList);
                rallyExcelDTO.setDataLabel("全部数据");
                for (SqfwRallySign s : sqfwRallySigns) {
                    int num = sqfwRallySigns.indexOf(s);
                    s.setNum(String.valueOf(num + 1));
                    s.setDate(DateUtil.format(s.getCreateDate(), DatePattern.CHINESE_DATE_TIME_PATTERN ));
                    s.setRallyName(rally.getRallyName());
                    s.setDept(rally.getRallyReleaseDept());
                    s.setActivityFunds( rallyPdfSaveDTO.getActivityFunds()/100);
                    if (Validator.isCitizenId(s.getCardId())) {
                        int age = IdcardUtil.getAgeByIdCard(s.getCardId());
                        s.setAge(String.valueOf(age));
                    }else {
                        s.setGender("-");
                        s.setAge("-");
                        s.setSpecialMark("-");
                    }
                    if (ObjectUtil.isNotEmpty(s.getSignFalg()) && 1 == s.getSignFalg()) {
                        s.setSignFalgString("已签到");
                    } else {
                        s.setSignFalgString("未签到");
                    }
                    // 需求：30天内未签到，未签到数量大于等于3，标记为”*“
                    String remark = timeOutPhoneMap.get(s.getPhone() + s.getName());
                    if (StringUtils.isNotEmpty(remark)
                            && ((ObjectUtil.isNotEmpty(s.getSignFalg()) && 0 == s.getSignFalg()) || ObjectUtil.isEmpty(s.getSignFalg()))) {
                        s.setRemark(remark);
                    } else {
                        s.setRemark("-");
                    }
                    RallyPdfSaveDTO bean=JSONUtil.toBean(s.getInfoJson(),RallyPdfSaveDTO.class);
                    if(CollectionUtil.isNotEmpty(rallyPdfSaveDTO.getCustom())& CollectionUtil.isNotEmpty(bean.getCustom())){
                        s.setDataDynamic(StrFormatter.format("{dataDynamic.var{}}",s.getId()));
                        bean.getCustom().forEach(h->{
                            Map<String, Object> data=new HashMap<>();
                            data.put(StrFormatter.format("var{}",s.getId()),h.getDefaultVal());
                            dataDynamic.add(data);
                        });
                    }
                }
            } else {
                SqfwRallySign rallySign = new SqfwRallySign();
                sqfwRallySigns.add(rallySign);
                rallyExcelDTO.setDataLabel("暂无数据");
                return rallyExcelDTO;
            }

            List<Map<String, Object>> dataMap = new ArrayList<>();
            sqfwRallySigns.forEach(s -> {
                Map<String, Object> objectMap = BeanUtil.beanToMap(s);
                dataMap.add(objectMap);
            });
            Map<String, Object> sheetMap = new HashMap<>();
            sheetMap.put("sheetDept", rally.getRallyName());
            sheetMap.put("head", "活动报名信息表");
            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("rallyTemp.xlsx");

            //zhangp 2023-12-21 文件名统一
            String fileName = DownLoadFileNameEnum.ACTIVITY_PERSON_INFO.getName() + DateUtil.format(new Date(), "yyyyMMddHHmmss");
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(sheetMap);
            excelFileCreateParam.setDataList(dataMap);
            excelFileCreateParam.setClazz(SqfwRallySignDTO.class);
            excelFileCreateParam.setFileName(fileName);
            excelFileCreateParam.setDataDynamic(dataDynamic);
            String excelUrl = CommonFormOfficeUtils.dynamicExcelAndUpload(excelFileCreateParam);
            rallyExcelDTO.setExcelUrl(excelUrl);
            rallyExcelDTO.setTemplageUrl(NutzIocUtils.getOssPrefix()+"/blankStencil/rally/【活动人员报名信息表】台账.xlsx");
            return rallyExcelDTO;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(StrFormatter.format("生成" + DownLoadFileNameEnum.ACTIVITY_PERSON_INFO.getName() + "表错误"));
        }

        return null;
    }
    //非世纪城
    @Override
    public RallyExcelDTO generateRallyExcelV2(String rallyId) {
        RallyExcelDTO rallyExcelDTO=new RallyExcelDTO();
        rallyExcelDTO.setExcelNname("活动人员报名信息表");
        try {
            Sqfw_rally rally = this.fetch(rallyId);
            if (ObjectUtil.isEmpty(rally)) {
                throw new ServiceException("无法找到对象");
            }

            List<SqfwRallySign> sqfwRallySigns = sqfwRallySignService.query(Cnd.where(SqfwRallySign::getRallyId, "=", rallyId));
            if (CollUtil.isNotEmpty(sqfwRallySigns)) {
                // 统计未签到情况 add by LJT 2024-01-25
                List<Sqfw_rally_sign> sqfwRallySignsList = new ArrayList<Sqfw_rally_sign>();
                sqfwRallySigns.forEach(source -> {
                    Sqfw_rally_sign target = new Sqfw_rally_sign();
                    BeanUtil.copyProperties(source, target);
                    sqfwRallySignsList.add(target);
                });
                Map<String, String> timeOutPhoneMap = sqfwRallySignForAdminService.checkSignTimeout(sqfwRallySignsList);
                rallyExcelDTO.setDataLabel("全部数据");
                for (SqfwRallySign s : sqfwRallySigns) {
                    int num = sqfwRallySigns.indexOf(s);
                    s.setNum(String.valueOf(num + 1));
                    s.setDate(DateUtil.format(rally.getCreateTime(), "yyyy-MM-dd"));
                    s.setRallyName(rally.getRallyName());
                    s.setDept(rally.getRallyReleaseDept());
                    if (Validator.isCitizenId(s.getCardId())) {
                        int age = IdcardUtil.getAgeByIdCard(s.getCardId());
                        s.setAge(String.valueOf(age));
                    }else {
                        s.setGender("-");
                        s.setAge("-");
                        s.setSpecialMark("-");
                    }
                    if (ObjectUtil.isNotEmpty(s.getSignFalg()) && 1 == s.getSignFalg()) {
                        s.setSignFalgString("已签到");
                    } else {
                        s.setSignFalgString("未签到");
                    }
                    // 需求：30天内未签到，未签到数量大于等于3，标记为”*“
                    String remark = timeOutPhoneMap.get(s.getPhone() + s.getName());
                    if (StringUtils.isNotEmpty(remark)
                            && ((ObjectUtil.isNotEmpty(s.getSignFalg()) && 0 == s.getSignFalg()) || ObjectUtil.isEmpty(s.getSignFalg()))) {
                        s.setRemark(remark);
                    } else {
                        s.setRemark("-");
                    }
                }
            } else {
                SqfwRallySign rallySign = new SqfwRallySign();
                sqfwRallySigns.add(rallySign);
                rallyExcelDTO.setDataLabel("暂无数据");
                return rallyExcelDTO;
            }


            List<Map<String, Object>> dataMap = new ArrayList<>();
            sqfwRallySigns.forEach(s -> {
                Map<String, Object> objectMap = BeanUtil.beanToMap(s);
                dataMap.add(objectMap);
            });
            Map<String, Object> sheetMap = new HashMap<>();
            sheetMap.put("sheetDept", rally.getRallyName());
            String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("rallyTempForAdmin.xlsx");

            //String fileName = "报名信息表" + DateUtil.format(new Date(), "yyyyMMddHHmmss");

            //zhangp 2023-12-21 文件名统一
            String fileName = DownLoadFileNameEnum.ACTIVITY_PERSON_INFO.getName() + DateUtil.format(new Date(), "yyyyMMddHHmmss");

            ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
            excelFileCreateParam.setTemplatePath(templatePath);
            excelFileCreateParam.setSheetParam(sheetMap);
            excelFileCreateParam.setDataList(dataMap);
            excelFileCreateParam.setClazz(SqfwRallySignDTO.class);
            excelFileCreateParam.setFileName(fileName);
            String excelUrl = CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
            rallyExcelDTO.setExcelUrl(excelUrl);
            rallyExcelDTO.setTemplageUrl(NutzIocUtils.getOssPrefix()+"/blankStencil/rally/【活动人员报名信息表】台账.xlsx");
            return rallyExcelDTO;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(StrFormatter.format("生成" + DownLoadFileNameEnum.ACTIVITY_PERSON_INFO.getName() + "表错误"));
        }

        return null;
    }



    @Override
    public List<SqfwRallySign> getPeople(String rallyId) {
        List<SqfwRallySign> sqfwRallySigns = sqfwRallySignService.query(Cnd.where(SqfwRallySign::getRallyId, "=", rallyId));
        sqfwRallySigns.forEach(s -> {
            if (ObjectUtil.isEmpty(s.getSignFalg())) {
                s.setSignFalg(0);
            }
        });
        return sqfwRallySigns;
    }

    @Override
    public Pagination getListPage(Sqfw_rally sqfwRally) {
        Cnd cnd = Cnd.NEW();
        Sys_user user = sysUserService.getUserById(SecurityUtil.getUserId());
        if(StrUtil.isEmpty(sqfwRally.getCommunity())){
            sqfwRally.setCommunity(user.getCommunity());

        }
        if(StrUtil.isEmpty(sqfwRally.getCounty())){
            sqfwRally.setCounty(user.getCounty());
        }
        if(StrUtil.isNotBlank(sqfwRally.getCounty())&&!sqfwRally.getCounty().equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCounty,"=",sqfwRally.getCounty());
            cnd.and(e1);
        }
        if(StrUtil.isNotBlank(sqfwRally.getCommunity())&&!sqfwRally.getCommunity().equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCommunity,"=",sqfwRally.getCommunity());
            cnd.and(e1);
        }
        if(StrUtil.isNotBlank(sqfwRally.getQuery())){
            SqlExpressionGroup e1 = Cnd.exps("rally_content_json ->>'eventRegistrationTime'","like","%"+sqfwRally.getQuery()+"%");
            SqlExpressionGroup e2 = Cnd.exps("rally_content_json ->>'siteOfActivity'","like","%"+sqfwRally.getQuery()+"%");
            SqlExpressionGroup e3 = Cnd.exps(Sqfw_rally::getRallyName,"like","%"+sqfwRally.getQuery()+"%");
            cnd.and(e1.or(e2).or(e3));
        }
        if(ObjectUtil.isNotEmpty(sqfwRally.getRallyStartTime())&&ObjectUtil.isNotEmpty(sqfwRally.getRallyEndTime())){
            cnd.and(Sqfw_rally::getCreateTime,"between",new Object[]{sqfwRally.getRallyStartTime(), sqfwRally.getRallyEndTime()});
        }

        if(StrUtil.isNotBlank(sqfwRally.getDraft())){
            if("1".equals(sqfwRally.getDraft())){
                cnd.and(Sqfw_rally::getDraft,"=",sqfwRally.getDraft())
                        .and(Sqfw_rally::getRallyReleaseUserId,"=",SecurityUtil.getUserId());
            }else if("2".equals(sqfwRally.getDraft())){
                cnd.and(Cnd.exps(Sqfw_rally::getDraft,"=","2").or(Cnd.exps(Sqfw_rally::getDraft,"is",null)))
                        .and(Sqfw_rally::getRallyReleaseUserId,"=",SecurityUtil.getUserId());;
            }
        }else {
            cnd.and(Cnd.exps(Sqfw_rally::getDraft,"=","2").or(Cnd.exps(Sqfw_rally::getDraft,"is",null)));
        }
        if(StrUtil.isNotBlank(sqfwRally.getActivityType())){
            cnd.and("rally_content_json ->>'activityType'","=",sqfwRally.getActivityType());
        }
        if(StrUtil.isNotBlank(sqfwRally.getTypeOfService())){
            cnd.and("rally_content_json ->>'typeOfService'","=",sqfwRally.getTypeOfService());
        }
        if(StrUtil.isNotEmpty(sqfwRally.getFinishStatus())){
            if("new".equals(sqfwRally.getFinishStatus())){
                cnd.and("rally_content_json ->>'eventRegistrationClosingTime'",">",DateUtil.format(DateUtil.offsetMinute(new Date(), 3),DatePattern.NORM_DATETIME_FORMAT));
            }else if("complete".equals(sqfwRally.getFinishStatus())){
                cnd.and("rally_content_json ->>'eventRegistrationClosingTime'","<=",DateUtil.format(DateUtil.offsetMinute(new Date(), 3),DatePattern.NORM_DATETIME_FORMAT));
            }
        }

        if(ObjectUtil.isEmpty(sqfwRally.getRallyType())){
            cnd.and(Sqfw_rally::getRallyType,"in", Arrays.asList("2","3"));
        }else {
            cnd.and(Sqfw_rally::getRallyType,"=", String.valueOf(sqfwRally.getRallyType()));
        }
        if(StrUtil.isEmpty(sqfwRally.getOrderBy())){
            cnd.orderBy(Sqfw_rally::getEventRegistrationTime,sqfwRally.getSort());
        }else {
            cnd.orderBy(sqfwRally.getOrderBy(),sqfwRally.getSort());
        }

        return this.listPage(sqfwRally.getPageNo(),sqfwRally.getPageSize(), cnd);

    }



    @Override
    public SqfwRallySign arriveMark(String id) {
        SqfwRallySign sqfwRallySign = sqfwRallySignService.fetch(id);
        if (ObjectUtil.isEmpty(sqfwRallySign)) {
            throw new ServiceException("无法查找到对象");
        }
        if (ObjectUtil.isEmpty(sqfwRallySign.getSignFalg()) || 0 == sqfwRallySign.getSignFalg()) {
            sqfwRallySign.setSignFalg(1);
        } else {
            sqfwRallySign.setSignFalg(0);
        }
        sqfwRallySignService.updateIgnoreNull(sqfwRallySign);
        return sqfwRallySign;
    }

    @Override
    public void arriveMarkBatch(List<String>  ids) {
        sqfwRallySignService.update(Chain.make("signFalg",1),Cnd.where(SqfwRallySign::getId,"in",ids));
    }


    @Override
    public void manyArriveMark(String rallyId) {
        Sql sql = Sqls.create("UPDATE public.sqfw_rally_sign SET sign_falg=1 where rally_id =@rallyId");
        sql.params().set("rallyId", rallyId);
        sqfwRallySignService.execute(sql);
    }

    @Override
    public List<TemplateFileDTO> changeTemplate(List<String> ids) {
        List<TemplateFileDTO> meetingFileDTOList=new ArrayList<>();
        Cnd cnd1 = Cnd.NEW();
        Cnd cnd2 = Cnd.NEW();
        Criteria cri = Cnd.cri();
        SqlExpressionGroup criSql = new SqlExpressionGroup();
        criSql.and("1","=","1");
        Sys_user user = sysUserService.getUserById(SecurityUtil.getUserId());

        if(StrUtil.isNotBlank(user.getCounty())&&!user.getCounty().equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCounty,"=",user.getCounty());
            cnd1.and(e1);
            criSql.andEquals(Sqfw_rally::getCounty, user.getCounty());
        }
        if(StrUtil.isNotBlank(user.getCommunity())&&!user.getCommunity().equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCommunity,"=",user.getCommunity());
            cnd1.and(e1);
            criSql.andEquals(Sqfw_rally::getCommunity, user.getCommunity());
        }
        // 增加条件满足后台需求，add by LJT 2024-02-01
        if (ObjectUtil.isNotEmpty(ids) && ids.size() > 0) {
            cnd1.and(Sqfw_rally::getId, "in", ids);
            criSql.and(Sqfw_rally::getId, "in", ids);
        }
        int  rallyCount =   this.count(cnd1);

        int  zdhdCount =  this.count(cnd1.and(" rally_content_json ->>'focusOrNot'","=","1"));

        Cnd cnd3 = Cnd.NEW();
        SqlExpressionGroup volunteerOrNotExp = Cnd.exps(" rally_content_json ->>'volunteerOrNot'","=","1");
        cnd3.and(criSql).and(volunteerOrNotExp);
        int  zyfwCount =   this.count(cnd3);

        cnd3 = Cnd.NEW();
        SqlExpressionGroup yikebaoOrNotExp = Cnd.exps(" rally_content_json ->>'yikebaoOrNot'","=","1");
        cnd3.and(criSql).and(yikebaoOrNotExp);
        int  ykb =   this.count(cnd3);

        cnd3 = Cnd.NEW();
        SqlExpressionGroup activityTypeExp = Cnd.exps(" trim(rally_content_json->>'activityType') ","!="," ");
        cnd3.and(criSql).and(activityTypeExp);
        int  xsd =   this.count(cnd3);

        cnd3 = Cnd.NEW();
        SqlExpressionGroup typeOfServiceExp = Cnd.exps(" trim(rally_content_json->>'typeOfService') ","!="," ");
        cnd3.and(criSql).and(typeOfServiceExp);
        int  zhx =   this.count(cnd3);

        String gzjh="暂无数据";
        String ydhd="暂无数据";
        String zdhdLabe="暂无数据";
        String zyfwLabe="暂无数据";
        String ykbLabe="暂无数据";
        String zhxLabe="暂无数据";
        String xsdLabe="暂无数据";
        if(rallyCount>0){
            gzjh="全部数据";
            ydhd="部分数据";
        }
        if(zdhdCount>0){
            zdhdLabe="部分数据";
        }
        if(zyfwCount>0){
            zyfwLabe="部分数据";
        }
        if(ykb>0){
            ykbLabe="部分数据";
        }
        if(xsd>0){
            xsdLabe="部分数据";
        }
        if(zhx>0){
            zhxLabe="部分数据";
        }
        for (SqfwRallyEnums enumObj : SqfwRallyEnums.values()) {
            if (enumObj.isLoopLable()) {
                TemplateFileDTO templateFileDTO = new TemplateFileDTO();
                templateFileDTO.setCode(enumObj.getCode());
                templateFileDTO.setFileNname(enumObj.getName());
                templateFileDTO.setPngUrl(NutzIocUtils.getOssPrefix()+enumObj.getPreview());
                templateFileDTO.setTemplageUrl(NutzIocUtils.getOssPrefix()+enumObj.getTemplatUrl());
                if(enumObj.getCode()== SqfwRallyEnums.WORK_PLAN.getCode()){
                    templateFileDTO.setDataLabel(gzjh);
                }else if(enumObj.getCode()==SqfwRallyEnums.KEY_ACTIVITY_TASK.getCode()){
                    templateFileDTO.setDataLabel(zdhdLabe);
                }else if(enumObj.getCode()==SqfwRallyEnums.MONTHLY_ACTIVITY_PLAN.getCode()){
                    templateFileDTO.setDataLabel(ydhd);
                }else if(enumObj.getCode()==SqfwRallyEnums.A.getCode()){
                    templateFileDTO.setDataLabel(ykbLabe);
                }else if(enumObj.getCode()==SqfwRallyEnums.B.getCode()){
                    templateFileDTO.setDataLabel(xsdLabe);
                }else if(enumObj.getCode()==SqfwRallyEnums.C.getCode()){
                    templateFileDTO.setDataLabel(zhxLabe);
                }else {
                    templateFileDTO.setDataLabel(zyfwLabe);
                }
                meetingFileDTOList.add(templateFileDTO);
            }
        }
        return meetingFileDTOList;
    }


    @Override
    public Pagination generateDetailFile(SummaryQueryReqVo reqVo) {
        Pagination pagination = new Pagination();
        List<DownFileTypesResVo> meetingFileDTOList=new ArrayList<>();
        Cnd cnd1 = Cnd.NEW();
        SqlExpressionGroup criSql = new SqlExpressionGroup();
        if(StrUtil.isNotBlank(reqVo.getFillStartDate())&&StrUtil.isNotBlank(reqVo.getFillEndDate())){
            criSql.andBetween(Sqfw_rally::getEventRegistrationTime, reqVo.getFillStartDate() + " 00:00:00", reqVo.getFillEndDate() + " 23:59:59");
        }else{
            criSql.and("1","=","1");
        }

        Sys_user user = sysUserService.getUserById(SecurityUtil.getUserId());

        if(StrUtil.isNotBlank(user.getCounty())&&!user.getCounty().equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCounty,"=",user.getCounty());
            cnd1.and(e1);
            criSql.andEquals(Sqfw_rally::getCounty, user.getCounty());
        }
        if(StrUtil.isNotBlank(user.getCommunity())&&!user.getCommunity().equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCommunity,"=",user.getCommunity());
            cnd1.and(e1);
            criSql.andEquals(Sqfw_rally::getCommunity, user.getCommunity());
        }

        int  rallyCount =   this.count(cnd1);

        int  zdhdCount =   this.count(cnd1.and(" rally_content_json ->>'focusOrNot'","=","1"));

        Cnd cnd3 = Cnd.NEW();
        SqlExpressionGroup volunteerOrNotExp = Cnd.exps(" rally_content_json ->>'volunteerOrNot'","=","1");
        cnd3.and(criSql).and(volunteerOrNotExp);
        int  zyfwCount =   this.count(cnd3);

        cnd3 = Cnd.NEW();
        SqlExpressionGroup yikebaoOrNotExp = Cnd.exps(" rally_content_json ->>'yikebaoOrNot'","=","1");
        cnd3.and(criSql).and(yikebaoOrNotExp);
        int  ykb =   this.count(cnd3);

        cnd3 = Cnd.NEW();
        SqlExpressionGroup activityTypeExp = Cnd.exps(" trim(rally_content_json->>'activityType') ","!="," ");
        cnd3.and(criSql).and(activityTypeExp);
        int  xsd =   this.count(cnd3);

        cnd3 = Cnd.NEW();
        SqlExpressionGroup typeOfServiceExp = Cnd.exps(" trim(rally_content_json->>'typeOfService') ","!="," ");
        cnd3.and(criSql).and(typeOfServiceExp);
        int  zhx =   this.count(cnd3);

        String gzjh="暂无数据";
        String ydhd="暂无数据";
        String zdhdLabe="暂无数据";
        String zyfwLabe="暂无数据";
        String ykbLabe="暂无数据";
        String zhxLabe="暂无数据";
        String xsdLabe="暂无数据";
        if(rallyCount>0){
            gzjh="全部数据";
            ydhd="部分数据";
        }
        if(zdhdCount>0){
            zdhdLabe="部分数据";
        }
        if(zyfwCount>0){
            zyfwLabe="部分数据";
        }
        if(ykb>0){
            ykbLabe="部分数据";
        }
        if(xsd>0){
            xsdLabe="部分数据";
        }
        if(zhx>0){
            zhxLabe="部分数据";
        }
        for (SqfwRallyEnums enumObj : SqfwRallyEnums.values()) {
            if (enumObj.isLoopLable()) {
                DownFileTypesResVo templateFileDTO = new DownFileTypesResVo();
                templateFileDTO.setFileCode(String.valueOf(enumObj.getCode()) );
                templateFileDTO.setFileName(enumObj.getName());
                templateFileDTO.setPreviewImage(Arrays.asList(NutzIocUtils.getOssPrefix()+enumObj.getPreview()));
                templateFileDTO.setTemplageUrl(NutzIocUtils.getOssPrefix()+enumObj.getTemplatUrl());
                templateFileDTO.setFileType("excel");
                if(enumObj.getCode()== SqfwRallyEnums.WORK_PLAN.getCode()){
                    templateFileDTO.setLabel(gzjh);
                }else if(enumObj.getCode()==SqfwRallyEnums.KEY_ACTIVITY_TASK.getCode()){
                    templateFileDTO.setLabel(zdhdLabe);
                }else if(enumObj.getCode()==SqfwRallyEnums.MONTHLY_ACTIVITY_PLAN.getCode()){
                    templateFileDTO.setLabel(ydhd);
                }else if(enumObj.getCode()==SqfwRallyEnums.A.getCode()){
                    templateFileDTO.setLabel(ykbLabe);
                }else if(enumObj.getCode()==SqfwRallyEnums.B.getCode()){
                    templateFileDTO.setLabel(xsdLabe);
                }else if(enumObj.getCode()==SqfwRallyEnums.C.getCode()){
                    templateFileDTO.setLabel(zhxLabe);
                }else {
                    templateFileDTO.setLabel(zyfwLabe);
                }
                meetingFileDTOList.add(templateFileDTO);
            }
        }
        pagination.setList(meetingFileDTOList);
        pagination.setTotalCount(rallyCount);
        return pagination;
    }

    @Override
    public TemplateFileDTO generateByCode(int code, String startTime,String endTime, List<String> ids) {
        TemplateFileDTO templateFileDTO=new TemplateFileDTO();
        String fileUrl= getDatBycode( code,  startTime,endTime, ids);
        templateFileDTO.setFileUrl(fileUrl);
        return templateFileDTO;
    }


    //选择区域匹配项
    private String getArea(Sys_user user) {
        if (StrUtil.isEmpty(user.getProvince()) ||
                StrUtil.isEmpty(user.getCity()) ||
                StrUtil.isEmpty(user.getDistrict()) ||
                StrUtil.isEmpty(user.getCounty()) ||
                StrUtil.isEmpty(user.getCounty())) {
            throw new ServiceException("当前登录用户所属区域为空");
        }
        StringBuilder builder = new StringBuilder();
        builder.append(user.getProvince());
        builder.append("-" + user.getCity());
        builder.append("-" + user.getDistrict());
        builder.append("-" + user.getCounty());
        builder.append("-" + user.getCommunity());
        return builder.toString();
    }

    private String getDatBycode(int code, String startTime,String endTime, List<String> ids) {
        SqfwRallyEnums sqfwRallyEnums=SqfwRallyEnums.getTemplatEnums(code);
        Sys_user user = sysUserService.getUserById(SecurityUtil.getUserId());
        SheetMapDTO sheetMapDTO=   BeanUtil.copyProperties(user,SheetMapDTO.class);
        sheetMapDTO.setDate(DateUtil.format(DateUtil.parse(startTime) ,"yyyy年"));
        List<Sqfw_rally> sqfwRallies=new ArrayList<>();
        Cnd cnd = Cnd.NEW();
        String community = user.getCommunity();
        String county = user.getCounty();

//        types.add(SqfwRallyEnum.JSONBY.getCode());
//        types.add(SqfwRallyEnum.JSONBN.getCode());
        cnd.and(Sqfw_rally::getRallyType,"in",Arrays.asList("2","3"));
        // 增加条件满足后台需求，add by LJT 2024-02-01
        if (ObjectUtil.isNotEmpty(ids) && ids.size() > 0) {
            cnd.and(Sqfw_rally::getId, "in", ids);
        }
        if(StrUtil.isNotBlank(community)&&!community.equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCommunity,"=",community);
            cnd.and(e1);
        }
        if(StrUtil.isNotBlank(county)&&!county.equals("直属")){
            SqlExpressionGroup e1 = Cnd.exps(Sqfw_rally::getCounty,"=",county);
            cnd.and(e1);
        }
        if (StringUtils.isNotEmpty(startTime)&&StringUtils.isNotEmpty(endTime)) {
            cnd.and("to_char( eventRegistrationTime,'YYYY-MM-DD')",">=",startTime)
                    .and("to_char( eventRegistrationTime,'YYYY-MM-DD')","<=",endTime);
        }
        if(StringUtils.isEmpty(startTime)){
            startTime = DateUtil.format(new Date(),DatePattern.NORM_DATETIME_FORMAT);
        }
        cnd.orderBy(Sqfw_rally::getEventRegistrationTime,"asc");
        switch (code){
            case 1:
            case 3:
                sqfwRallies=   this.query(cnd);
                break;
            case 2:
                sqfwRallies=   this.query(cnd.and(" rally_content_json ->>'focusOrNot'","=","1"));
                break;
            case 4:
            case 5:
                sqfwRallies=   this.query(cnd.and("rally_content_json ->>'volunteerOrNot'","=","1"));
                break;
            case 6:
                sqfwRallies=   this.query(cnd.and("rally_content_json ->>'yikebaoOrNot'","=","1"));
                break;
            case 7:
                sqfwRallies=   this.query(cnd.and("trim(rally_content_json->>'activityType') ","!="," "));
                break;
            case 8:
                sqfwRallies=   this.query(cnd.and(" trim(rally_content_json->>'typeOfService') ","!="," "));
                break;
        }
        if(CollectionUtil.isEmpty(sqfwRallies)){
            throw new BaseException("所选时间暂无数据!");
        }

        List<SqfwRallyUpdateDTO> sqfwRallyUpdateDTOS =new ArrayList<>();
        sqfwRallies.forEach(s->{
            SqfwRallyUpdateDTO sqfwRallyUpdateDTO=new SqfwRallyUpdateDTO();
            BeanUtil.copyProperties(s,sqfwRallyUpdateDTO);
            sqfwRallyUpdateDTOS.add(sqfwRallyUpdateDTO);
        });

        List<SqfwRallyUpdateDTO> finalSqfwRallies = sqfwRallyUpdateDTOS;
        switch (code){
            case 1:
            case 2:
                sheetMapDTO.setDate(DateUtil.format(DateUtil.parse(startTime) ,"yyyy年"));
                sqfwRallyUpdateDTOS.forEach(s->{
                    int num = finalSqfwRallies.indexOf(s);
                    RallyPdfSaveDTO rallyPdfSaveDTO=JSONUtil.toBean(s.getRallyContentJson(),RallyPdfSaveDTO.class);
                    rallyPdfSaveDTO.setRemark(" ");
                    rallyPdfSaveDTO.setEventRegistrationTime(DateUtil.format(DateUtil.parse(rallyPdfSaveDTO.getEventRegistrationTime()) ,"yyyy年MM月dd日"));
                    rallyPdfSaveDTO.setNum(num+1);
                    s.setRallyPdfSaveDTO(rallyPdfSaveDTO);
                });
                break;
            case 3:
                sheetMapDTO.setDate(DateUtil.format(DateUtil.parse(startTime) ,"MM月"));
                sqfwRallyUpdateDTOS.forEach(s->{
                    int num = finalSqfwRallies.indexOf(s);
                    RallyPdfSaveDTO rallyPdfSaveDTO=JSONUtil.toBean(s.getRallyContentJson(),RallyPdfSaveDTO.class);
                    rallyPdfSaveDTO.setEventRegistrationTime(DateUtil.format(DateUtil.parse(rallyPdfSaveDTO.getEventRegistrationTime()) ,"yyyy年MM月dd日 HH:mm"));
                    rallyPdfSaveDTO.setEventRegistrationClosingTime(DateUtil.format(DateUtil.parse(rallyPdfSaveDTO.getEventRegistrationClosingTime()) ,"yyyy年MM月dd日 HH:mm"));
                    rallyPdfSaveDTO.setNum(num+1);
                    s.setRallyPdfSaveDTO(rallyPdfSaveDTO);
                });
                break;

            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
                sheetMapDTO.setDate(DateUtil.format(new Date(),"yyyy年MM月"));
                sqfwRallyUpdateDTOS.forEach(s->{
                    int num = finalSqfwRallies.indexOf(s);
                    RallyPdfSaveDTO rallyPdfSaveDTO=JSONUtil.toBean(s.getRallyContentJson(),RallyPdfSaveDTO.class);
                    rallyPdfSaveDTO.setEventRegistrationTime(DateUtil.format(DateUtil.parse(rallyPdfSaveDTO.getEventRegistrationTime()) ,"yyyy年MM月dd日 HH:mm"));
                    rallyPdfSaveDTO.setEventRegistrationClosingTime(DateUtil.format(DateUtil.parse(rallyPdfSaveDTO.getEventRegistrationClosingTime()) ,"yyyy年MM月dd日 HH:mm"));
                    rallyPdfSaveDTO.setNum(num+1);
                    rallyPdfSaveDTO.setRemark(" ");
                    s.setRallyPdfSaveDTO(rallyPdfSaveDTO);
                });
                break;
        }


        List<Map<String, Object>> dataMap = new ArrayList<>();
        sqfwRallyUpdateDTOS.forEach(s -> {
            Map<String, Object> objectMap = BeanUtil.beanToMap(s.getRallyPdfSaveDTO());
            dataMap.add(objectMap);
        });
        String fileName = sqfwRallyEnums.getName() + DateUtil.format(new Date(), "yyyyMMddHHmmss");
        String templatePath = CommonFormOfficeUtils.getTemplatePathByOS(sqfwRallyEnums.getTemplat());
        ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
        excelFileCreateParam.setTemplatePath(templatePath);
        excelFileCreateParam.setSheetParam(BeanUtil.beanToMap(sheetMapDTO));
        excelFileCreateParam.setDataList(dataMap);
        excelFileCreateParam.setClazz(RallyPdfSaveDTO.class);
        excelFileCreateParam.setFileName(fileName);
        String excelUrl = CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
        return excelUrl;
    }



    private void pushRally(Sqfw_rally sqfwRally){
        RallyPdfSaveDTO rallyPdfSaveDTO=JSONUtil.toBean(sqfwRally.getRallyContentJson(),RallyPdfSaveDTO.class);

        NutMap paramMap = NutMap.NEW();
        long timestamp = System.currentTimeMillis();
        String password = DigestUtils.md5Hex("Admin@123");
        String verifyCode= StrUtil.format("account=sqxz0626&password={}&uid={}",password,timestamp);
        paramMap.addv("appId",23)
                .addv("account","sqxz0626")
                .addv("password",password)
                .addv("uid",timestamp)
                .addv("verifyCode", new SM3Util().hash1(verifyCode))
                .addv("psComplex",0)
                .addv("openApiAccess",1);
        String getTokenUrl= "https://xwm.gywb.cn/gw/cms/admin/login";
        Result result = JSONUtil.toBean(HttpUtil.post(getTokenUrl, JSONUtil.toJsonStr(paramMap)),Result.class);
        if(!result.isSuccess()){
            return;
        }
        String token = (String) ((JSONObject) result.getData()).get("token");

        List<NutMap> DD =   JSONUtil.toList(sqfwRally.getRallyContent(),NutMap.class);
        StringBuilder content = new StringBuilder();
        if(CollectionUtil.isNotEmpty(DD)){
            for (NutMap nutMap : DD) {
                if(nutMap.get("type").equals("text")){
                    content.append(StrUtil.format("<p>{}</p><br/>",nutMap.get("content")));
                }
                if(nutMap.get("type").equals("img")){
                    content.append(StrUtil.format("<div class=\"media-wrap image-wrap\"><img src=\"{}\"/></div>",nutMap.get("content")));
                }

            }
        }else {
            content.append(StrUtil.format("<p>{}</p><br/>",rallyPdfSaveDTO.getEventProfile()));
        }


        ApiRallyModelDTO apiRallyModelDTO = new ApiRallyModelDTO();
        apiRallyModelDTO.setContent(JSONUtil.toJsonStr(content));
        apiRallyModelDTO.setActivityTitle(sqfwRally.getRallyName());
        apiRallyModelDTO.setActivityStartTime(rallyPdfSaveDTO.getEventRegistrationTime());
        apiRallyModelDTO.setActivityEndTime(rallyPdfSaveDTO.getEventRegistrationClosingTime());
        apiRallyModelDTO.setPhone(rallyPdfSaveDTO.getPhone());
        apiRallyModelDTO.setType("0c2de57a-eb48-42d6-9857-f6f1b58a5bd5");
        if(StrUtil.isNotEmpty(sqfwRally.getPeoLimit())){
            apiRallyModelDTO.setMassesNum(Integer.valueOf(sqfwRally.getPeoLimit()));
        }
        apiRallyModelDTO.setActivityPlace(rallyPdfSaveDTO.getSiteOfActivity());
        apiRallyModelDTO.setContacts(sqfwRally.getRallyReleaseUserName());
        long betweenDay = DateUtil.between( DateUtil.parse(rallyPdfSaveDTO.getEventRegistrationTime()), DateUtil.parse(rallyPdfSaveDTO.getEventRegistrationClosingTime()), DateUnit.HOUR);

        apiRallyModelDTO.setActivityDuration(Integer.valueOf((int) betweenDay));
        apiRallyModelDTO.setDeptName(sqfwRally.getDistrict()+sqfwRally.getCounty()+sqfwRally.getCommunity());

        String createUrl= "https://xwm.gywb.cn/gw/cms/real_activity/create";
        HashMap<String, String> headers  = new HashMap<>();//存放请求头，可以存放多个请求头
        headers.put("token",token);
        String result2 =  HttpUtil.createPost(createUrl).addHeaders(headers).body(JSONUtil.toJsonStr(apiRallyModelDTO)).execute().body();
        System.out.println(result2);
    }

    public static void main(String[] args) {


        long betweenDay = DateUtil.between( DateUtil.parse("2024-07-08 10:18:32.474"), DateUtil.parse("2024-07-08 09:01:39.165"), DateUnit.MINUTE);

        System.out.println(Integer.valueOf((int) betweenDay)/60);




    }
}
